﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1.segments.data_type
{
    class CN
    {
        private StringBuilder id_name;
        private StringBuilder Family_name;
        private StringBuilder Given_name;
        private StringBuilder Middle_name;
        private StringBuilder Suffix;
        private StringBuilder Prefix;
        private StringBuilder Degree;
        private StringBuilder Source_table;
        private HD Assigning_authority;

        public StringBuilder ID_name { get; set; }
        public StringBuilder family_name { get; set; }
        public StringBuilder given_name { get; set; }
        public StringBuilder middle_name { get; set; }
        public StringBuilder suffix { get; set; }
        public StringBuilder prefix { get; set; }
        public StringBuilder degree { get; set; }
        public StringBuilder source_table { get; set; }
        public HD assigning_authority { get; set; }

        public CN()
        {
            id_name = new StringBuilder();
            family_name = new StringBuilder();
            given_name = new StringBuilder();
            middle_name = new StringBuilder();
            suffix = new StringBuilder();
            prefix = new StringBuilder();
            degree = new StringBuilder();
            source_table = new StringBuilder();
            Assigning_authority = new HD();
        }

        enum segments { DG1, EVN, MSH, OBX, ORC, PID, PV1, RXO, NTE, OBR }

        string check(string source, int temp)
        {

            if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.MSH.ToString())
                return segments.MSH.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.DG1.ToString())
                return segments.DG1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.EVN.ToString())
                return segments.EVN.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.NTE.ToString())
                return segments.NTE.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBR.ToString())
                return segments.OBR.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBX.ToString())
                return segments.OBX.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.ORC.ToString())
                return segments.ORC.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PID.ToString())
                return segments.PID.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PV1.ToString())
                return segments.PV1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.RXO.ToString())
                return segments.RXO.ToString();

            return null;
        }


        public int fill(string source, int temp)
        {
            int i, x = 0;
            for (i = temp; i < source.Length; i++)
            {
                if (check(source, i) != null)
                {
                    id_name.Capacity = id_name.Length;
                    family_name.Capacity = family_name.Length;
                    given_name.Capacity = given_name.Length;
                    middle_name.Capacity = middle_name.Length;
                    suffix.Capacity = suffix.Length;
                    prefix.Capacity = prefix.Length;
                    degree.Capacity = degree.Length;
                    source_table.Capacity = source_table.Length;
                    return i - 1;
                }
               else if (source[i] == '^')
                    x++;
                else if (source[i] == '|')
                {
                    id_name.Capacity = id_name.Length;
                    family_name.Capacity = family_name.Length;
                    given_name.Capacity = given_name.Length;
                    middle_name.Capacity = middle_name.Length;
                    suffix.Capacity = suffix.Length;
                    prefix.Capacity = prefix.Length;
                    degree.Capacity = degree.Length;
                    source_table.Capacity = source_table.Length;
                    return i;
                }

                else if (x == 0)
                    id_name.Insert(id_name.Length, source[i]);
                else if (x == 1)
                    family_name.Insert(family_name.Length, source[i]);
                else if (x == 2)
                    given_name.Insert(given_name.Length, source[i]);
                else if (x == 3)
                    middle_name.Insert(middle_name.Length, source[i]);
                else if (x == 4)
                    suffix.Insert(suffix.Length, source[i]);
                else if (x == 5)
                    prefix.Insert(prefix.Length, source[i]);
                else if (x == 6)
                    degree.Insert(degree.Length, source[i]);
                else if (x == 7)
                    source_table.Insert(source_table.Length, source[i]);
                else if (x == 8)
                    i=assigning_authority.fill(source, i);

            }
            return i;
        }

        public override string ToString()
        {
            string cn_string = "";

            if (id_name != null)
                cn_string += id_name;
            if (Family_name != null)
                cn_string += "^" + Family_name;
            if (Given_name != null)
                cn_string += Given_name;
            if (Middle_name != null)
                cn_string += "^" + Middle_name;
            if (Suffix != null)
                cn_string += Suffix;
            if (Prefix != null)
                cn_string += "^" + Prefix;
            if (Degree != null)
                cn_string += Degree;
            if (Source_table != null)
                cn_string += "^" + Source_table;
            if (assigning_authority != null)
                cn_string += assigning_authority.tostring();
            
            return cn_string;
        }
    }
}
